<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <div>
    <div style="height: 100px;"></div>
    <div id="nav" style="background: red;height: 30px;box-sizing: border-box; top:0;z-index:9999;width: 100%;"> </div>
    <div style="background: skyblue;height: 500px;box-sizing: border-box;"></div>
    <div style="background: yellowgreen;height: 800px;box-sizing: border-box;"></div>
  </div>
</body>
<script>
  let mynav = document.getElementById('nav')
  window.onscroll = () => {
    if (document.documentElement.scrollTop > mynav.offsetTop) {
      mynav.style.position = "fixed";
    } else {
      mynav.style.position = "static";
    }
  };
</script>

</html>